package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/api/v1/author")
public class AuthorController{
    @Autowired
    private AuthorService service;

    /**
     * 根据名称查询作者是否存在     != 0 则被注册
     * @param name
     * @return
     */
    @GetMapping("/findByUserName/{name}")
    public int findByUserName(@PathVariable("name") String name) {
        LambdaQueryWrapper<ApAuthor> wrapper = Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name);
        return service.count(wrapper);
    }

    @GetMapping("/findAuthorByName/{id}")
    public ResponseResult findAuthorByName(@PathVariable("id") String id) {
        ApAuthor author = service.getById(id);
        return ResponseResult.okResult(author);
    }

    /**
     * 返回1 保存成功
     * @param apAuthor
     * @return
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        apAuthor.setCreatedTime(new Date());
        service.save(apAuthor);
        return ResponseResult.okResult(1);
    }
}
